#include "DateFormatDlg.h"

#include <QtCore/QDateTime>

int DateFormatDlg::curRow_ = -1;

DateFormatDlg::DateFormatDlg() {
	formats_ << "hh:mm:ss" << "h:mm:ssAP" 
			<< "dd.MM.yyyy" << "yyyy-MM-dd" << "dd/MM/yyyy" << "dd/MM/yy" << "MM/dd/yy" 
			<< "MMMM d yyyy" << "d MMMM yyyy" 
			<< "MMMM d yyyy, hh:mm" << "d MMMM yyyy, hh:mm" 
			<< "dddd MMMM d yyyy" << "dddd d MMMM yyyy" 
			<< "ddd MMM d yyyy" << "ddd d MMM yyyy" 
			<< "hh:mm:ss dd.MM.yyyy" <<"dd.MM.yyyy hh:mm:ss";

	ui.setupUi(this);

	connect(ui.list_, SIGNAL(itemDoubleClicked(QListWidgetItem*)), SLOT(itemDoubleClicked(QListWidgetItem*)));

	QDateTime dt = QDateTime::currentDateTime();
	foreach (QString fmt, formats_)
		ui.list_->addItem(dt.toString(fmt));
	
	if ( curRow_ >= 0 && curRow_ < ui.list_->count() )
		ui.list_->setCurrentRow(curRow_);
}

QString DateFormatDlg::format() const {
	int index = ui.list_->currentRow();
	if ( index >= 0 ) {
		curRow_ = index;
		return formats_.at(index);
	}
	else
		return "";
}

void DateFormatDlg::itemDoubleClicked(QListWidgetItem* it) {
	if ( it )
		accept();
}
